<template>
  <div class="app-container">
    <el-alert
      title="修改密码须知"
      style="margin-bottom:20px"
      type="warning"
      description="密码修改成功后，您的账号将被登出，请使用新密码重新登录"
      show-icon
      :closable="false"
    />
    <el-form ref="form" :rules="rules" :model="form" label-width="150px" style="width: 450px;">
      <el-form-item label="名称">
        <el-input v-model="name" disabled />
      </el-form-item>
      <el-form-item label="账号">
        <el-input v-model="username" disabled />
      </el-form-item>
      <el-form-item label="当前密码" prop="old">
        <el-input v-model="form.old" type="password" placeholder="请输入当前旧密码" />
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="form.pass" type="password" placeholder="请输入新密码" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="form.checkPass" type="password" placeholder="请重新输入新密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="btnLoading" @click="onSubmit">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { password } from '@/api/admin'

export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value.length < 6) {
        callback(new Error('新密码长度不得少于6位'))
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        old: '',
        pass: '',
        checkPass: ''
      },
      btnLoading: false,
      rules: {
        old: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'username'
    ])
  },
  created () {},
  methods: {
    onSubmit () {
      var _this = this
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          password({ oldp: this.form.old, newp: this.form.checkPass }).then(res => {
            this.$alert(res.msg, {
              type: 'success',
              confirmButtonText: '我知道了',
              callback: action => {
                _this.$store.dispatch('admin/logout').then(() => {
                  location.reload()
                })
              }
            })
            this.form.old = ''
            this.form.pass = ''
            this.form.checkPass = ''
            this.btnLoading = false
          }).catch(err => {
            console.log(err)
            this.btnLoading = false
          })
        }
      })
    }
  }
}
</script>

